import os
import uuid
from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import render, render_to_response
from huffman_tools import huffman as huf
from huffman_tools import tools as tool


# Create your views here.
def huffman_encode(request):
    return render(request, 'huffman.html')


def upload(request):
    ctx = {}
    if request.method == "POST":
        file_name = str(request.FILES['file'])
        if not file_name.endswith('txt'):
            ctx['fail'] = 'file format exist wrong!'
        else:
            file = request.FILES['file']
            ctx['success'] = 'Successful'
            input_string = tool.read_file(tool.save_file(file))
            char_store, freq_store = tool.count_frequency(input_string)
            char_frequency = tool.get_char_frequency(char_store, freq_store)
            nodes = huf.create_nodes([item[1] for item in char_frequency])
            root = huf.create_huffman_tree(nodes)
            codes = huf.huffman_encoding(nodes, root)
            save_file_name = tool.get_huffman_file(input_string, char_frequency, codes)
            for item in zip(char_frequency, codes):
                print('Character:%s freq:%-2d   encoding: %s', item[0][0], item[0][1], item[1])
            ctx['node'] = char_frequency

            def file_iterator(files, chunk_size=512):
                with open(files) as f:
                    while True:
                        c = f.read(chunk_size)
                        if c:
                            yield c
                        else:
                            break
            the_file_name = tool.get_encode_ration(codes)+'_'+str(uuid.uuid1())+'.txt'
            response = StreamingHttpResponse(file_iterator(save_file_name))
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
            return response

def download(request):
    ctx = {}
    if request.method == "POST":
        code_table, code_file = str(request.FILES['code_table']), str(request.FILES['code_file'])
        if not code_table.endswith('txt') and code_file.endswith('txt'):
            ctx['fail'] = 'file format exist wrong!'
        else:
            file_one = request.FILES['code_table']
            file_two = request.FILES['code_file']
            two_string = tool.read_file(tool.save_file(file_two))
            char_store, freq_store = tool.read_code_table(tool.save_file(file_one))
            out_string = tool.decode_huffman(two_string, char_store, freq_store)
            save_file_name = tool.save_file(out_string)

            def file_iterator(files, chunk_size=512):
                with open(files) as f:
                    while True:
                        c = f.read(chunk_size)
                        if c:
                            yield c
                        else:
                            break
            the_file_name = str(uuid.uuid1())+'.txt'
            response = StreamingHttpResponse(file_iterator(save_file_name))
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
            return response
